package com.briup.es.web;

import com.briup.es.auth.SecurityUserContext;
import com.briup.es.config.response.Result;
import com.briup.es.domain.bean.User;
import com.briup.es.domain.dto.UserDTO;
import com.briup.es.domain.dto.UserLoginDTO;
import com.briup.es.service.UserService;
import com.briup.es.validate.Add;
import com.briup.es.validate.Update;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class AuthController {

    private final UserService userService;

    @PostMapping("/register")
    public Result<?> register(@RequestBody @Validated(Add.class) UserDTO userRegisterDTO){
        userService.register(userRegisterDTO);
        return Result.success();
    }

    @PostMapping("/login")
    public Result<?> login(@RequestBody @Valid UserLoginDTO loginDTO){
        Map<String,Object> map = userService.login(loginDTO);
        return Result.success(map);
    }

    @PutMapping("/updateUser")
    public Result<?> updateUser(@RequestBody @Validated(Update.class) UserDTO userDTO){
        userService.updateUser(userDTO);
        return Result.success();
    }

    @GetMapping("/getUserInfo")
    public Result<?> getUserInfo(){
        User user = userService.queryById(SecurityUserContext.currentUserId());
        return Result.success(user);
    }
}
